layui.config({
    base: '/static/js/lib',
    version: new Date().getTime()
}).use(['layer', "table"], function () {

    $ = layui.jquery;
    var layer = layui.layer;
    var table = layui.table;
    var form = layui.form;
    form.on('submit(sreach)', function (data) {
        tableRender(data.field);
        return false;
    });

    function tableRender(queryCondition) {
        var url = "/course/data/all/page?";
        for (var key in queryCondition) {
            var condition = queryCondition[key];
            if (null != condition && condition != "") {
                url += ("&" + key + "=" + condition);
            }
        }
        table.render({
            elem: '#course-page',
            height: 472,
            url: url,
            page: true,
            cols: [[ //表头
                {field: 'id', title: 'ID', width: "10%", fixed: 'left'}
                , {field: 'name', title: '课程名称', width: "30%"}
                , {field: 'desc', title: '课程描述', width: "25%"}
                , {field: 'createTime', title: '创建时间', width: "12%"}
                , {field: 'updateTime', title: '最后更新时间', width: "12%"}
                , {field: 'exist', title: '操作', width: "10%", templet: "#exist"}
            ]]
        });
    }

    tableRender({});

    $("#cateName").focus(function () {
        layer.open({
            type: 2,
            area: ['400px', '500px'],
            maxmin: true,
            moveOut: false,
            shade: 0.4,
            async: true,
            title: '选择课程分类',
            content: "/courseCate/list"
        });
    });

    window.cateTreeCallBack = function (node) {
        if (node.isSubCate) {
            $("#cateName").val(node.name);
            $("input[name='cateId']").val(node.id);
            $("input[name='cateIdPath']").val(node.catePath);
            return {isClose: true};
        }
        return {
            isClose: false,
            callback: function () {
                layer.msg("只能选择子分类");
            }
        };
    };

    //监听工具条
    table.on('tool(course-page)', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent == "course-add") {
            layer.open({
                type: 2,
                area: ['100%', '100%'],
                fix: false,
                maxmin: true,
                shadeClose: true,
                shade: 0.4,
                title: "添加课程",
                content: "/course/add?id=" + data.id
            });
        }
    });
});